/* Copyright */
/*******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Tsunagi-BPM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************/
 /* Copyright End */
 
package tsunagi.json.path.condition;

import tsunagi.json.path.Tokenizer;

public class ConditionHandler {
	private Conditions conditions;
	private Conditions backupConditions;
	private boolean currentResult = true;
	
	public void ConditionParser(Conditions cond) {
		this.setConditions(cond);
	}
	protected Conditions getConditions() {
		return conditions;
	}
	protected void setConditions(Conditions conditions) {
		this.conditions = conditions;
	}
	
	public Conditions getBackupConditions() {
		return backupConditions;
	}
	public void setBackupConditions(Conditions backupConditions) {
		this.backupConditions = backupConditions;
	}
	public boolean isCurrentResult() {
		return currentResult;
	}
	
	public void setCurrentResult(boolean currentResult) {
		this.currentResult = currentResult;
	}
	
	public void startEvaluation(String token) {
		this.setCurrentResult(true);
		int i1 = token.indexOf(Tokenizer.DELIM_ARRAY_COND_START);
		int i2 = token.indexOf(Tokenizer.DELIM_ARRAY_COND_END);
		String cond = token.substring(i1 + 1, i2);
		Tokenizer tokenizer = new Tokenizer(cond, Condition.AND);
		Conditions conditions = new Conditions();
		while((token = tokenizer.next()) != null) {
			i1 = token.indexOf("=");
			String key = token.substring(1, i1-1);
			String val = token.substring(i1 + 2, token.length()-1);
			Condition condition = new Condition(Condition.EQUAL, key, val);
			conditions.push(condition);
			conditions.push(Condition.AND);
		}
		conditions.removeFirst(); // remove the last AND ope
		this.setBackupConditions(conditions);
	}
	
	public void initConditionCheck() {
		this.setConditions(this.getBackupConditions().copy());
	}
	
	/**
	 * It returns null when there is no more condition. Subsequently,
	 * the caller should call isCurrentResult method to see the condtion's
	 * evaluation.
	 * 
	 * @param previousResult
	 * @return
	 */
	public Condition next() {
		Condition result = null;
		Object obj = null;
		try {
			obj = this.getConditions().pop();
		} catch (Exception e) {
			return result;
		}
		if (obj instanceof Condition) {
			result = (Condition) obj;
		} else if (Condition.AND.equals((String)obj)) {
			if (this.isCurrentResult()) {
				result = (Condition)this.getConditions().pop();
			} else {
				this.setCurrentResult(false);
			}
		}
		return result;
	}
}
